package com.sicent.operate.common.util;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

public class ImageUtil {
	
	public static void zoom(InputStream srcInputStreamImage, File destImageFile, int width, int height, String imageFormat) throws IOException {
		ImageIO.write(zoom(ImageIO.read(srcInputStreamImage), width, height),imageFormat, destImageFile);
	}
	
	public static BufferedImage zoom(BufferedImage srcBufferedImage, int width, int height) {
		int imgWidth = srcBufferedImage.getWidth();
		int imgHeight = srcBufferedImage.getHeight();
		if (imgWidth > width || imgHeight > height) {
			if (Double.valueOf(imgHeight) / imgWidth >= Double.valueOf(height) / width) {
				imgWidth = (int) Math.round(Double.valueOf(height) / imgHeight * imgWidth);
				imgHeight = height;
			} else {
				imgHeight = (int) Math.round(Double.valueOf(width) / imgWidth * imgHeight);
				imgWidth = width;
			}
		}
		BufferedImage destBufferedImage = new BufferedImage(width, height, srcBufferedImage.getType());
		Graphics2D g = destBufferedImage.createGraphics();
		g.setBackground(Color.WHITE);
		g.clearRect(0, 0, width, height);
		g.drawImage(srcBufferedImage.getScaledInstance(imgWidth, imgHeight, Image.SCALE_SMOOTH), 0, 0, width, height, null);
		g.dispose();
		return destBufferedImage;
	}
	
	public final static void scale(InputStream srcInputStreamImage,
			File destImageFile, double scale, String imageFormat)
			throws IOException {
		ImageIO.write(scale(srcInputStreamImage, scale), imageFormat,
				destImageFile);
	}
	
	public final static BufferedImage scale(InputStream srcInputStreamImage,
			double scale) throws IOException {
		return scale(ImageIO.read(srcInputStreamImage), scale);
	}
	
	public final static BufferedImage scale(BufferedImage srcBufferedImage,
			double scale) {
		int width = (int) Math.round(srcBufferedImage.getWidth() * scale);
		int height = (int) Math.round(srcBufferedImage.getHeight() * scale);
		Image image = srcBufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		BufferedImage destBufferedImage = new BufferedImage(width, height, srcBufferedImage.getType());
		Graphics graphics = destBufferedImage.getGraphics();
		graphics.drawImage(image, 0, 0, null);
		graphics.dispose();
		return destBufferedImage;
	}
	
	public final static void generateSmall(InputStream inputStream, String fileName, String parentPath) throws IOException{
		double totalScale = inputStream.available()/(50*1024.0);
		BufferedImage bufferedImage = ImageIO.read(inputStream);
		int width = bufferedImage.getWidth();
		int height = bufferedImage.getHeight();
		width = (int)(width/Math.sqrt(totalScale));
		height = (int)(height/Math.sqrt(totalScale));
		File smallfile = new File(parentPath + File.separator + fileName);
		if (!smallfile.exists()){
			smallfile.createNewFile();
		}
		Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		BufferedImage destBufferedImage = new BufferedImage(width, height, bufferedImage.getType());
		Graphics graphics = destBufferedImage.getGraphics();
		graphics.drawImage(image, 0, 0, null);
		graphics.dispose();
		FileOutputStream out = new FileOutputStream(smallfile);
		ImageIO.write(destBufferedImage, fileName.substring(fileName.lastIndexOf(".") + 1), out);
        out.close();
	}

}
